package com.trsj.lnsj.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;

public class CommTool {

  /**
   * 两对象之间的拷贝(在目标对象中存在的所有set方法，如果在源对象中存在对应的get方法，且源对象的get方法的返回值不为null,就进行拷贝) 仅拷贝方法名及方法返回类型完全一样的属性值
   * 
   * @param desc 目标对象
   * @param orig 源对象
   */
  public static void copyPropertiesNotForce(Object desc, Object orig) {
    Class origClass = orig.getClass();
    Class descClass = desc.getClass();

    Method[] descMethods = descClass.getMethods();
    Method[] origMethods = origClass.getMethods();

    Map methodMap = new HashMap();
    for (int i = 0; i < origMethods.length; i++) {
      Method method = origMethods[i];
      String methodName = method.getName();
      if (!methodName.equals("getClass") && methodName.startsWith("get")
          && (method.getParameterTypes() == null || method.getParameterTypes().length == 0)) {
        methodMap.put(methodName, method);
      }
    }
    for (int i = 0; i < descMethods.length; i++) {
      Method method = descMethods[i];
      String methodName = method.getName();
      if (!methodName.equals("getClass") && methodName.startsWith("get")
          && (method.getParameterTypes() == null || method.getParameterTypes().length == 0)) {
        if (methodMap.containsKey(methodName)) {
          Method origMethod = (Method) methodMap.get(methodName);
          try {
            if (method.getReturnType().equals(origMethod.getReturnType())) {
              Object returnObj = origMethod.invoke(orig, null);
              if (returnObj == null) {
                continue;
              }
              String field = methodName.substring(3);
              String setMethodName = "set" + field;
              Method setMethod =
                  descClass.getMethod(setMethodName, new Class[] {method.getReturnType()});
              setMethod.invoke(desc, new Object[] {returnObj});
            }
          } catch (IllegalArgumentException e) {
            e.printStackTrace();
          } catch (IllegalAccessException e) {
            e.printStackTrace();
          } catch (InvocationTargetException e) {
            e.printStackTrace();
          } catch (SecurityException e) {
            e.printStackTrace();
          } catch (NoSuchMethodException e) {
            e.printStackTrace();
          }
        }
      }
    }
  }

  /**
   * 两对象之间的拷贝(在目标对象中存在的所有set方法，如果在源对象中存在对应的get方法，且源对象的get方法的返回值不为null和空,就进行拷贝) 仅拷贝方法名及方法返回类型完全一样的属性值
   * 
   * @param desc 目标对象
   * @param orig 源对象
   */
  public static void copyPropertiesNotNullandEmpty(Object desc, Object orig) {
    Class origClass = orig.getClass();
    Class descClass = desc.getClass();

    Method[] descMethods = descClass.getMethods();
    Method[] origMethods = origClass.getMethods();

    Map methodMap = new HashMap();
    for (int i = 0; i < origMethods.length; i++) {
      Method method = origMethods[i];
      String methodName = method.getName();
      if (!methodName.equals("getClass") && methodName.startsWith("get")
          && (method.getParameterTypes() == null || method.getParameterTypes().length == 0)) {
        methodMap.put(methodName, method);
      }
    }
    for (int i = 0; i < descMethods.length; i++) {
      Method method = descMethods[i];
      String methodName = method.getName();
      if (!methodName.equals("getClass") && methodName.startsWith("get")
          && (method.getParameterTypes() == null || method.getParameterTypes().length == 0)) {
        if (methodMap.containsKey(methodName)) {
          Method origMethod = (Method) methodMap.get(methodName);
          try {
            if (method.getReturnType().equals(origMethod.getReturnType())) {
              Object returnObj = origMethod.invoke(orig, null);
              if (returnObj == null || ("").equals(returnObj.toString())) {
                continue;
              }
              String field = methodName.substring(3);
              String setMethodName = "set" + field;
              Method setMethod =
                  descClass.getMethod(setMethodName, new Class[] {method.getReturnType()});
              setMethod.invoke(desc, new Object[] {returnObj});
            }
          } catch (IllegalArgumentException e) {
            e.printStackTrace();
          } catch (IllegalAccessException e) {
            e.printStackTrace();
          } catch (InvocationTargetException e) {
            e.printStackTrace();
          } catch (SecurityException e) {
            e.printStackTrace();
          } catch (NoSuchMethodException e) {
            e.printStackTrace();
          }
        }
      }
    }
  }

  /**
   * 
   * 获取请求IP地址 @Title: getIpAddr @Description: TODO @param @param request @param @return 设定文件 @return
   * String 返回类型 @throws
   */
  public static String getIpAddr(HttpServletRequest request) {
    String ip = request.getHeader("x-forwarded-for");
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
      ip = request.getHeader("Proxy-Client-IP");
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
      ip = request.getHeader("WL-Proxy-Client-IP");
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
      ip = request.getRemoteAddr();
    }
    return ip;
  }
}
